package com.movie.act;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.movie.util.Log;
import com.movie.util.RoateUtil;
import com.movie.util.ScreenInfo;
import com.movie.view.GalleryFlow1;

public class CinemaDesAct extends BaseActivity {

	private Context mContext = CinemaDesAct.this;
	private LinearLayout wenzibg = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cinemadesact);
		wenzibg = (LinearLayout) findViewById(R.id.wenzibg);
		initBackTitleBar();
		initGalleryFlow1();
		onclick();
		RoateUtil.tranlateLittile(mContext, paiqil);
		RoateUtil.tranlateDown(mContext, wenzibg);
	}

	private TextView gomap = null;
	private TextView gophone = null;
	private TextView paiqi = null;
	private LinearLayout paiqil = null;

	public void release() {
		if (paiqil != null) {
			paiqil.removeAllViews();
			paiqil = null;
		}
	}

	private void onclick() {

		paiqil = (LinearLayout) findViewById(R.id.paiqil);
		gomap = (TextView) findViewById(R.id.gomap);
		gophone = (TextView) findViewById(R.id.gophone);
		paiqi = (TextView) findViewById(R.id.paiqi);
		gomap.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(mContext, OneCinaemaAct.class);
				mContext.startActivity(intent);
			}
		});
		gophone.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction("android.intent.action.CALL");
				intent.setData(Uri.parse("tel:" + gophone.getText().toString()));// mobile为你要拨打的电话号码，模拟器中为模拟器编号也可
				Log.i("--gophone----------" + gophone.getText().toString());
				startActivity(intent);
			}
		});

		paiqi.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(mContext, MoviePrepareAct.class);
				mContext.startActivity(intent);
			}
		});
		;
	}

	private void initGalleryFlow1() {
		Integer[] images = { R.drawable.imagetemp, R.drawable.imagetemp,
				R.drawable.imagetemp, R.drawable.imagetemp,
				R.drawable.imagetemp, R.drawable.imagetemp,
				R.drawable.imagetemp, R.drawable.imagetemp,
				R.drawable.imagetemp, R.drawable.imagetemp };
		// int length = 6;
		// Integer[] images = new Integer[length];
		// mImageViews = new ImageView[length];
		// LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
		// LinearLayout.LayoutParams.WRAP_CONTENT,
		// LinearLayout.LayoutParams.WRAP_CONTENT);
		// lp.setMargins(4, 0, 4, 0);
		// ImageView mImageView = null;
		// for (int i = 0; i < length; i++) {
		// images[i] = R.drawable.imagetemp;
		// mImageView = new ImageView(this);
		// mImageView.setLayoutParams(new LinearLayout.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		// mImageView.setScaleType(ImageView.ScaleType.CENTER);
		// mImageView.setImageResource(R.drawable.dot1);
		// mImageView.setLayoutParams(lp);
		// // ImageView mImageView=(ImageView)
		// // LayoutInflaterUtil.xmlToView(context, R.layout.image, null);
		// mImageViews[i] = mImageView;
		// car_type_dotLR.addView(mImageView);
		// }

		adapter = new ImageAdapter(this, images);
		adapter.createReflectedImages();
		gallery = (GalleryFlow1) findViewById(R.id.gallery);
		gallery.setLayoutParams(ScreenInfo.getCar_type__galleryRParams());
		gallery.setAdapter(adapter);
	}

	private ImageAdapter adapter = null;
	private GalleryFlow1 gallery = null;

	private ImageView[] mImageView = null;

	private class ImageAdapter extends BaseAdapter {

		int mGalleryItemBackground;
		private Context mContext;
		private Integer[] mImageIds;
		private ImageView[] mImages;

		public ImageAdapter(Context c, Integer[] ImageIds) {
			mContext = c;
			mImageIds = ImageIds;
			mImages = new ImageView[mImageIds.length];
		}

		public void releaseImageViews() {
			for (int i = 0; i < mImages.length; i++) {
				mImages[i].setImageBitmap(null);
				mImages[i].invalidate();
			}
		}

		public ImageView getImageItem(int i) {
			return mImages[i];
		}

		public boolean createReflectedImages() {
			// final int reflectionGap = 4;
			int index = 0;
			Bitmap originalImage = null;
			ImageView imageView = null;
			for (int imageId : mImageIds) {
				originalImage = BitmapFactory.decodeResource(
						mContext.getResources(), imageId);
				imageView = new ImageView(mContext);
				imageView.setImageBitmap(originalImage);
				// imageView.setLayoutParams(new GalleryFlow1.LayoutParams(200,
				// 140));
				imageView.setLayoutParams(ScreenInfo
						.getGalleryImageLayoutParams());
				imageView.setScaleType(ScaleType.FIT_XY);
				mImages[index++] = imageView;
			}
			return true;
		}

		public int getCount() {
			return mImageIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return mImages[position];
		}

		public float getScale(boolean focused, int offset) {
			return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
		}

	}

}
